package com.yzy.soft.system.freemarker.template;

import com.yzy.soft.system.freemarker.*;
import com.yzy.soft.system.freemarker.database.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class TemplateHtml extends TemplateKernel {
    private final static Logger logger= LoggerFactory.getLogger(TemplateDao.class);

    @Override
    public File generateFileType(FileInfo fileInfo, Table table, DBInfo dbInfo) throws Exception {
        HtmlFile htmlFile=(HtmlFile)fileInfo;
        final String templateName = "html-table-data-base.ftl";
        File file = new File(fileInfo.getDiskPath());
        if (!file.exists()) {
            file.createNewFile();
        }else{
            file.deleteOnExit();
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("package_name", htmlFile.getPackageName());
            dataMap.put("table_annotation", htmlFile.getAnnotation());
            dataMap.put("table_name", htmlFile.getFilename());
            dataMap.put("project_name", "");

        dataMap.put("title", htmlFile.getHtml().getTitle());
        dataMap.put("links", htmlFile.getHtml().getLinks());
        dataMap.put("base_url", htmlFile.getHtml().getBaseUrl());
        dataMap.put("base_name",htmlFile.getHtml().getBaseName());
        dataMap.put("base_table_id", htmlFile.getHtml().getBaseTableId());
        dataMap.put("addupdateurl", htmlFile.getHtml().getAddUpdateUrl());
        dataMap.put("tables", htmlFile.getHtml().getHtmlTables());

            String path = fileInfo.getDiskPath() +htmlFile.getFilename()+ htmlFile.getSuffix();
        System.out.println("TemplateHtml path:"+path);
            File controllerFile = new File(path);
            generateFileByTemplate(templateName, controllerFile,fileInfo, dataMap);
            return controllerFile;
    }

}
